import { Component, OnInit } from '@angular/core';
import {TipService} from "../../../services/tip.service";
import {ExtendHttpService} from "../../../services/extend-http.service";
import {Router} from "@angular/router";

@Component({
  selector: 'app-notice-list',
  templateUrl: './notice-list.component.html',
  styleUrls: ['./notice-list.component.css']
})
export class NoticeListComponent implements OnInit {

  tipType = 'error';
  totalPage = 1;
  start = 1;
  list = [];
  listFlag = false;
  constructor(
    private tipService: TipService,
    private extendHttp: ExtendHttpService,
    private router: Router
  ) { }

  ngOnInit() {
    cBack =()=> {
      $('#btn-back').trigger('click');
    }
    this.getData();
  }

  getData() {
    this.extendHttp.get('/agentApp/notice/findPage', {start: this.start}).subscribe(response => {
      let res = response.json();
      if (res['code'] === '0') {
        this.list = res.page.list;
        if(!res.page.list.length) {
          this.listFlag = true;
        }
      } else if (res['code'] == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, res['message']);
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 1500);
      } else {
        this.tipType = 'error';
        this.tipService.change(true, res['message']);
      }
    }, error => {
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

  loadMore() {
    this.extendHttp.get('/agentApp/notice/findPage', {start: this.start + 1}).subscribe(response => {
      let res = response.json();
      if (res['code'] === '0') {
        this.totalPage =res.page.totalPage;
        this.list = this.list.concat(res.page.list);
        this.start = res.page.pageNumber
      } else if (res['code'] == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, res['message']);
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 1500);
      } else {
        this.tipType = 'error';
        this.tipService.change(true, res['message']);
      }
    }, error => {
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }
}
